/*
 *  Copyright (C) <2009>  Pavel Vakeš <pavel.vakes@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <messages_dialog_item.h>

#include <KDebug>
#include <Plasma/Svg>
#include <Plasma/Delegate>

using namespace Plasma;

MessagesDialogItem::MessagesDialogItem(MailChecker * mail_checker) : QStandardItem()
{
	Plasma::Svg icon_main;
	icon_main.setImagePath("widgets/gx_mail_notify-normal");
    this->setData(KIcon(icon_main.pixmap()), Qt::DecorationRole);

    this->setData(mail_checker->accountName(), Qt::DisplayRole);
    this->setData("Not checked.", StatusRole);

    this->setData(mail_checker->timeToCheck(), TimeToCheckRole);
    this->setData(false, CheckNowHoverRole);

    this->setData(true, SubTitleMandatoryRole);
    this->setData(Plasma::Delegate::MainColumn, ScopeRole);
}

MessagesDialogItem::~MessagesDialogItem()
{
}

void MessagesDialogItem::setAction(MailChecker::Action action)
{
	switch (action) {
		case MailChecker::OPEN_CONNECTION:
			this->setData(tr2i18n("Opening connection ..."), StatusRole);
		break;

		case MailChecker::VERIFY_CERT:
			this->setData(tr2i18n("Verification of the certificate..."), StatusRole);
		break;

		case MailChecker::VERIFY_USER: case MailChecker::VERIFY_PASS:
			this->setData(tr2i18n("Authentication ..."), StatusRole);
		break;

		case MailChecker::GET_STAT:
			this->setData(tr2i18n("Reading messages ..."), StatusRole);
		break;

		case MailChecker::CLOSE_CONNECTION:
			this->setData(tr2i18n("Closing connection ..."), StatusRole);
		break;
	}

}

void MessagesDialogItem::setTimeToCheck(int time_to_check)
{
	this->setData(time_to_check, TimeToCheckRole);
}

void MessagesDialogItem::setMessages(int count)
{
	Plasma::Svg icon;
	icon.setImagePath("widgets/gx_mail_notify-" + QString(count > 0 ? "icon_new" : "normal"));
	this->setData(KIcon(icon.pixmap()), Qt::DecorationRole);

	if (count > 0) this->setData("You have " + QString::number(count) + " new message" + (count > 1 ? "s" : "") + ".", StatusRole);
	else this->setData("You have no new messages.", StatusRole);
}

void MessagesDialogItem::setError(QString error)
{
	Plasma::Svg icon_error;
	icon_error.setImagePath("widgets/gx_mail_notify-icon_error");
	this->setData(KIcon(icon_error.pixmap()), Qt::DecorationRole);

	this->setData(tr2i18n("Error: ") + error, StatusRole);
}

void MessagesDialogItem::checkNow() {
	emit checkNowClicked();
}

void MessagesDialogItem::runMailClient() {
	emit runMailClientClicked();
}
